﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Config;
using DLL.Entities.Event;
using DLL.Entities.Status;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace RAD
{
    public partial class Form1 : Form
    {

        int id=0, speed, altitude, heading, distance;
        string size;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //creates generic object test and Config object RADCON
            object test = new object();
            Config RADCON = new Config();
            // starts timer to send statud updates
            timer1.Start();

            try
            {
                //creates serializer of type Config, calls ReadXML() and deserializes RAD_Config.xml to return generic object "test"
                XmlSerializer serializer = new XmlSerializer(typeof(Config));
                Read_Write.ReadXML(out test, "RAD_Config.xml", serializer);
                // casts test to type Config and assigns to RADCON
                RADCON = (Config)test;

            }

            catch (Exception ex)
            {
                // writes exceptions to log file
                Utility.LogFile(ex.ToString());
            }
            //sets model and version text
            txt_Model.Text = RADCON.Model;
            txt_Version.Text = RADCON.Version;

        }

        private void btnTarget_Click(object sender, EventArgs e)
        {
            id ++;
            altitude = Utility.Random(5, 40000);
            speed = Utility.Random(10, 2200);
            heading = Utility.Random(1, 360);
            distance = Utility.Random(20, 50);
            string[] sizes = {"small", "medium", "large"};
            size = sizes[Utility.Random(0, 2)];

            if (altitude < 20)
            {
                speed = Utility.Random(1, 40);
            }
            else if (altitude >= 20 && altitude < 50)
            {
                speed = Utility.Random(10, 150);
                size = "small";
            }
            else 
            {
                if (size == "small")
                {
                    altitude = Utility.Random(50, 30000);
                    speed = Utility.Random(500, 2200);
                }
                else
                {
                    altitude = Utility.Random(50, 40000);
                    speed = Utility.Random(500, 1500);
                }
            }

            textID.Text = id.ToString();
            textSize.Text = size;
            textSpeed.Text = speed.ToString();
            textAltitude.Text = altitude.ToString();
            textHeading.Text = heading.ToString();
            textDistance.Text = distance.ToString();

        }

        private void btnLockOn_Click(object sender, EventArgs e)
        {
            RADEvent target = new RADEvent();
            target.id = id;
            target.size = size;
            target.speed = speed;
            target.altitude = altitude;
            target.heading = heading;
            target.distance = distance;

            XmlSerializer serializer = new XmlSerializer(typeof(RADEvent));
            Read_Write.WriteXML(target, "RAD_Event.xml", serializer);

            textID.Text = "";
            textSize.Text = "";
            textSpeed.Text = "";
            textAltitude.Text = "";
            textHeading.Text = "";
            textDistance.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RADStatus stat = new RADStatus();
            stat.timestamp = DateTime.Now;
            XmlSerializer serializer = new XmlSerializer(typeof(RADStatus));
            Read_Write.WriteXML(stat, "RAD_Status.xml", serializer);
        }
    }
}
